using System.Collections.Generic;
using System.ServiceModel.Syndication;

namespace ServiceModel.Syndication.LEAP2A {
    /// <summary>
    /// Useful methods for dealing with LEAP2A Atom feeds
    /// </summary>
    public static class LeapHelpers {
        /// <summary>
        /// Determines if an entry is solely part of entries in the specified feed.
        /// </summary>
        /// <param name="feed">The LEAP2A feed.</param>
        /// <param name="entry">The entry.</param>
        /// <returns>
        /// 	<c>true</c> if the entry is linked by other entries; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsAsset(LeapFeed feed, LeapItem entry) {
            bool partOf = false;
            bool isAsset = true;

            if (entry.IsComment) {
                isAsset = false;
            } else {
                foreach (LeapLink link in entry.Links) {
                    switch (link.LeapRelationshipType) {
                        case LeapPredicates.IsOutcomeOf:
                        case LeapPredicates.IsEvidenceOf:
                        case LeapPredicates.ReflectsOn:
                        case LeapPredicates.ReferencedBy:
                        case LeapPredicates.Attends:
                        case LeapPredicates.Supports:
                            isAsset = false;
                            break;
                        case LeapPredicates.IsPartOf:
                            partOf = true;
                            break;
                    }
                }
            }

            if (isAsset && partOf) {
                IList<LeapItem> linked = entry.GetLinkedByLinkType(LeapPredicates.IsPartOf);
                
                foreach (LeapItem item in linked) {
                    if (item.LeapType != LeapTypes.Selection) {
                        isAsset = false;
                    }
                }
            }

            return isAsset;
        }

        /// <summary>
        /// Gets the text from an AtomTextConstruct or a blank string.
        /// </summary>
        /// <param name="text">An AtomTextConstruct containing the text to be extracted.</param>
        /// <returns>A string containing the contents of the AtomTextConstruct or an empty string</returns>
        public static string GetAtomText(TextSyndicationContent text) {
            if (text != null) {
                if (text.Text != null) {
                    return text.Text;
                }
            }

            return "";
        }

        /// <summary>
        /// Gets the text from an AtomTextConstruct or a blank string.
        /// </summary>
        /// <param name="text">An AtomTextConstruct containing the text to be extracted.</param>
        /// <returns>A string containing the contents of the AtomTextConstruct or an empty string</returns>
        public static string GetAtomContent(SyndicationContent text) {
            TextSyndicationContent textContent = text as TextSyndicationContent;
            if (textContent != null) {
                if (textContent.Text != null) {
                    return textContent.Text;
                }
            }

            return "";
        }
    }
}
